import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Chess {
    public static final int CHESSBOARD_SIZE = 12;//棋盘大小12X12
    public static int FIRST = 0;//先手，1表示机器，-1表示人类
    Chessboard cboard = new Chessboard();
    private int flag1=0;//下棋
    private int flag2=0;//障碍
    private int firstboardx=-1,firstboardy=-1;//起子
    private int lastboardx=-1,lastboardy=-1;//落子
    private int barrierx=-1,barriery=-1;//障碍

    private int m1=5,m2=5,m3=5,m4=5,m5=5,m6=5,n1=5,n2=5,n3=5,n4=5,n5=5,n6=5;
    private ArrayList<String> chip = new ArrayList<>();//棋谱
    private  int qishu=0;//棋数
    private int[][] Chessboard = {{4,4,4,4,4,4,4,4,4,4,4,4},//棋盘1代表上方棋子皇后位置，-1代表下方棋子皇后位置
                                     {4,0,0,0,1,0,0,1,0,0,0,4},
                                     {4,0,0,0,0,0,0,0,0,0,0,4},
                                     {4,0,0,0,0,0,0,0,0,0,0,4},
                                     {4,1,0,0,0,0,0,0,0,0,1,4},
                                     {4,0,0,0,0,0,0,0,0,0,0,4},
                                     {4,0,0,0,0,0,0,0,0,0,0,4},
                                     {4,-1,0,0,0,0,0,0,0,0,-1,4},
                                     {4,0,0,0,0,0,0,0,0,0,0,4},
                                     {4,0,0,0,0,0,0,0,0,0,0,4},
                                     {4,0,0,0,-1,0,0,-1,0,0,0,4},
                                     {4,4,4,4,4,4,4,4,4,4,4,4},};
    //按钮事件监听器
    public void run(){
        cboard.paintchess();
        cboard.CFrame();
        for (int i = 1; i < 11; i++) {
            for (int j = 1; j < 11; j++) {
                int finalJ = j;
                int finalI = i;
                cboard.add[i][j].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(Chessboard[finalI][finalJ]==-1&&flag2==0){   //起子
                            flag1=1;
                            /*记下起子的位置*/
                            firstboardx=finalI;
                            firstboardy=finalJ;
                        }else if(Chessboard[finalI][finalJ]==0&&flag1==1&&((firstboardx==finalI&&firstboardy!=finalJ)||(firstboardx!=finalI&&firstboardy==finalJ)||(firstboardx-firstboardy==finalI-finalJ)||(firstboardx+firstboardy==finalI+finalJ))){  //落子
                            flag2=1;
                            /*记下落子的位置*/
                            lastboardx=finalI;
                            lastboardy=finalJ;

                            /*改变起子落子的参数*/
                            Chessboard[firstboardx][firstboardy]=0;
                            Chessboard[lastboardx][lastboardy]=-1;

                            /*改变起子落子的背景色*/
                            cboard.add[firstboardx][firstboardy].setBackground(Color.white);
                            cboard.add[lastboardx][lastboardy].setBackground(Color.blue);
                            flag1=0;
                        }else if(Chessboard[finalI][finalJ]==0&&flag2==1&&((lastboardx==finalI&&lastboardy!=finalJ)||(lastboardx!=finalI&&lastboardy==finalJ)||(lastboardx-lastboardy==finalI-finalJ)||(lastboardx+lastboardy==finalI+finalJ))){      //障碍
                            Chessboard[finalI][finalJ]=4;
                            cboard.add[finalI][finalJ].setBackground(Color.black);

                            Character s1 = null;
                            Character s2 = null;
                            Character s3 = null;

                            if(firstboardy==1){
                                s1='A';
                            }else if(firstboardy==2){
                                s1='B';
                            }else if(firstboardy==3){
                                s1='C';
                            }else if(firstboardy==4){
                                s1='D';
                            }else if(firstboardy==5){
                                s1='E';
                            }else if(firstboardy==6){
                                s1='F';
                            }else if(firstboardy==7){
                                s1='G';
                            }else if(firstboardy==8){
                                s1='H';
                            }else if(firstboardy==9){
                                s1='I';
                            }else if(firstboardy==10){
                                s1='J';
                            }

                            if(lastboardy==1){
                                s2='A';
                            }else if(lastboardy==2){
                                s2='B';
                            }else if(lastboardy==3){
                                s2='C';
                            }else if(lastboardy==4){
                                s2='D';
                            }else if(lastboardy==5){
                                s2='E';
                            }else if(lastboardy==6){
                                s2='F';
                            }else if(lastboardy==7){
                                s2='G';
                            }else if(lastboardy==8){
                                s2='H';
                            }else if(lastboardy==9){
                                s2='I';
                            }else if(lastboardy==10){
                                s2='J';
                            }

                            if(finalJ==1){
                                s3='A';
                            }else if(finalJ==2){
                                s3='B';
                            }else if(finalJ==3){
                                s3='C';
                            }else if(finalJ==4){
                                s3='D';
                            }else if(finalJ==5){
                                s3='E';
                            }else if(finalJ==6){
                                s3='F';
                            }else if(finalJ==7){
                                s3='G';
                            }else if(finalJ==8){
                                s3='H';
                            }else if(finalJ==9){
                                s3='I';
                            }else if(finalJ==10){
                                s3='J';
                            }

                            qishu++;
                            chip.add(" "+s1+""+(10-firstboardx+1)+s2+""+(10-lastboardx+1)+"("+s3+(10-finalI+1)+")");
                            System.out.println("起子："+firstboardx+","+firstboardy);
                            System.out.println("落子："+lastboardx+","+lastboardy);
                            System.out.println("障碍："+finalI+","+finalJ);
                            m1 = firstboardx;
                            m2 = firstboardy;
                            m3 = lastboardx;
                            m4 = lastboardy;
                            m5 = finalI;
                            m6 = finalJ;

                            firstboardx=-1;
                            firstboardy=-1;
                            flag2=0;
                            lastboardx=-1;
                            lastboardy=-1;

                            eliminate();
                            AI();   //调用AI算法
                            eliminate();
                        }
                    }
                });
            }
        }
        //我方先行按钮事件
        cboard.ourJbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AI();
            }
        });

        //对方先行按钮事件
        cboard.oppoisteJbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });

        //悔棋按钮事件
        cboard.regretJbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(n1!=5){
                    Chessboard[n1][n2] = 1;
                    Chessboard[n3][n4] = 0;
                    Chessboard[n5][n6] = 0;
                    cboard.add[n1][n2].setBackground(Color.red);
                    cboard.add[n3][n4].setBackground(Color.white);
                    cboard.add[n5][n6].setBackground(Color.white);
                }
                if(m1!=5){
                    Chessboard[m1][m2] =-1;
                    Chessboard[m3][m4] =0;
                    Chessboard[m5][m6] =0;
                    cboard.add[m1][m2].setBackground(Color.blue);
                    cboard.add[m3][m4].setBackground(Color.white);
                    cboard.add[m5][m6].setBackground(Color.white);
                }
                if(chip.size()<=1){
                    chip.remove(chip.size()-1);
                }
                else{
                    chip.remove(chip.size()-1);
                    chip.remove(chip.size()-1);
                }

            }
        });

        //保存棋谱按钮事件
        cboard.saveJbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    reserve();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });

        //结束按钮事件
        cboard.endJbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    //将棋谱写入txt文件中
    public  void reserve() throws Exception{
        FileWriter fileWriter=new FileWriter("C:\\Users\\lxl\\Desktop\\Result.txt");
        ArrayList<String> a=chip;
        int mn=0;
        int mn1=0;
        for (int i = 0; i < a.size(); i++) {
            ++mn;
            System.out.println("mn="+mn%2);
            if(mn%2==0){
                fileWriter.write(String.valueOf(a.get(i))+"\n");//换行转意
            }else {
                fileWriter.write((++mn1)+String.valueOf(a.get(i)));//换行转意
            }

        }
        fileWriter.flush();
        fileWriter.close();
    }

    //判断棋局胜负
    /*判断胜负手无法判断当两个棋子同时被阻隔时，判为负*/
    public void eliminate(){
        int sum1=0,sum2=0;
        for (int i = 1; i < 11; i++) {
            for (int j = 1; j < 11; j++) {
                 if(Chessboard[i][j]==-1&&Chessboard[i-1][j]==4&&Chessboard[i+1][j]==4&&Chessboard[i][j-1]==4&&Chessboard[i][j+1]==4&&Chessboard[i-1][j-1]==4&&Chessboard[i+1][j+1]==4&&Chessboard[i+1][j-1]==4&&Chessboard[i-1][j+1]==4){
                     sum1=sum1+1;
                 }else if(Chessboard[i][j]==1&&Chessboard[i-1][j]==4&&Chessboard[i+1][j]==4&&Chessboard[i][j-1]==4&&Chessboard[i][j+1]==4&&Chessboard[i-1][j-1]==4&&Chessboard[i+1][j+1]==4&&Chessboard[i+1][j-1]==4&&Chessboard[i-1][j+1]==4){
                     sum2=sum2+1;
                 }
            }
        }
        if(sum1==4){
            //电脑胜
            JOptionPane.showMessageDialog(null, "电脑胜", "提示",JOptionPane.ERROR_MESSAGE);
        }else if(sum2==4){
            //人胜
            JOptionPane.showMessageDialog(null, "YOU WIN!", "提示",JOptionPane.ERROR_MESSAGE);
        }
    }

    //程序核心AI算法
    public void AI(){
        Go gO = new Go();   //起子落子
        beginendChess begin = new beginendChess();  //起子
        beginendChess end = new beginendChess();    //落子
        beginendChess barr = new beginendChess();    //障碍

        //起子：搜索判断起子，并返回该棋子的坐标
        begin = bChess();
        System.out.println("begin:x="+begin.getX()+"y="+begin.getY());
        //落子：使用起子，使用算法判断落子位置，并返回该棋子的坐标
        end = eChess(begin);



        Chessboard[begin.getX()][begin.getY()] = 0;
        cboard.add[begin.getX()][begin.getY()].setBackground(Color.white);
        Chessboard[end.getX()][end.getY()] = 1;
        cboard.add[end.getX()][end.getY()].setBackground(Color.red);
        //障碍：传入落子，根据算法判断障碍位置，并返回障碍

        System.out.println("end:x="+end.getX()+"y="+end.getY());
        barr = barrier(end);
        Chessboard[barr.getX()][barr.getY()] = 4;
        cboard.add[barr.getX()][barr.getY()].setBackground(Color.black);
        Character s1 = null;
        Character s2 = null;
        Character s3 = null;

        if(begin.getY()==1){
            s1='A';
        }else if(begin.getY()==2){
            s1='B';
        }else if(begin.getY()==3){
            s1='C';
        }else if(begin.getY()==4){
            s1='D';
        }else if(begin.getY()==5){
            s1='E';
        }else if(begin.getY()==6){
            s1='F';
        }else if(begin.getY()==7){
            s1='G';
        }else if(begin.getY()==8){
            s1='H';
        }else if(begin.getY()==9){
            s1='I';
        }else if(begin.getY()==10){
            s1='J';
        }

        if(end.getY()==1){
            s2='A';
        }else if(end.getY()==2){
            s2='B';
        }else if(end.getY()==3){
            s2='C';
        }else if(end.getY()==4){
            s2='D';
        }else if(end.getY()==5){
            s2='E';
        }else if(end.getY()==6){
            s2='F';
        }else if(end.getY()==7){
            s2='G';
        }else if(end.getY()==8){
            s2='H';
        }else if(end.getY()==9){
            s2='I';
        }else if(end.getY()==10){
            s2='J';
        }

        if(barr.getY()==1){
            s3='A';
        }else if(barr.getY()==2){
            s3='B';
        }else if(barr.getY()==3){
            s3='C';
        }else if(barr.getY()==4){
            s3='D';
        }else if(barr.getY()==5){
            s3='E';
        }else if(barr.getY()==6){
            s3='F';
        }else if(barr.getY()==7){
            s3='G';
        }else if(barr.getY()==8){
            s3='H';
        }else if(barr.getY()==9){
            s3='I';
        }else if(barr.getY()==10){
            s3='J';
        }
        chip.add("  "+s1+""+(10-begin.getX()+1)+s2+""+(10-end.getX()+1)+"("+s3+(10-barr.getX()+1)+")");
        System.out.println("begin:x="+begin.getX()+"y="+begin.getY());
        System.out.println("end:x="+end.getX()+"y="+end.getY());
        System.out.println("barr:x="+barr.getX()+"y="+barr.getY());

        n1 = begin.getX();
        n2 = begin.getY();
        n3 = end.getX();
        n4 = end.getY();
        n5 = barr.getX();
        n6 = barr.getY();

        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 12; j++) {
                System.out.print(Chessboard[i][j]+"  ");
            }
            System.out.println();
        }
    }

    //起子
    public beginendChess bChess(){
        int index=0;
        int flag = 101;//找出步数最小可行棋

        beginendChess beginendchess = new beginendChess();
        for (int i = 1; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                //AI棋子
                int sum=0;
                int index1;
                if(Chessboard[i][j]==1){
                    index++;
                    //上
                    for (int m = i-1; m>0 ; m--) {
                        if(Chessboard[m][j]==0){
                            sum=sum+1;
                        }else if(Chessboard[m][j]==1||Chessboard[m][j]==-1||Chessboard[m][j]==4){
                            break;
                        }
                    }

                    //下
                    for (int k = i+1; k < 11; k++) {
                        if(Chessboard[k][j]==0){
                            sum=sum+1;
                        }else if(Chessboard[k][j]==1||Chessboard[k][j]==-1||Chessboard[k][j]==4){
                            break;
                        }
                    }

                    //左
                    for (int k = j-1; k >0; k--) {
                        if(Chessboard[i][k]==0){
                            sum=sum+1;
                        }else if(Chessboard[i][k]==1||Chessboard[i][k]==-1||Chessboard[i][k]==4){
                            break;
                        }
                    }
                    //右
                    for (int k = j+1; k < 11; k++) {
                        if(Chessboard[i][k]==0){
                            sum=sum+1;
                        }else if(Chessboard[i][k]==1||Chessboard[i][k]==-1||Chessboard[i][k]==4){
                            break;
                        }
                    }

                    //左上
                    if(i>=j){
                        index1=i;
                        for (int k = j-1; k > 0; k--) {
                            if(Chessboard[--index1][k]==0){
                                sum=sum+1;
                            }else if(Chessboard[index1][k]==1||Chessboard[index1][k]==-1||Chessboard[index1][k]==4){
                                break;
                            }
                        }
                    }else{
                        index1=j;
                        for (int k = i-1; k > 0; k--) {
                            if(Chessboard[k][--index1]==0){
                                sum=sum+1;
                            }else if(Chessboard[k][index1]==1||Chessboard[k][index1]==-1||Chessboard[k][index1]==4){
                                break;
                            }
                        }
                    }
                    //右下
                    if(i>=j){
                        index1=j;
                        for (int k = i+1; k < 11; k++) {
                            if(Chessboard[k][++index1]==0){
                                sum=sum+1;
                            }else if(Chessboard[k][index1]==1||Chessboard[k][index1]==-1||Chessboard[k][index1]==4){
                                break;
                            }
                        }
                    }else {
                        index1=i;
                        for (int k = j+1; k < 11; k++) {
                            if(Chessboard[++index1][k]==0){
                                sum=sum+1;
                            }else if(Chessboard[index1][k]==1||Chessboard[index1][k]==-1||Chessboard[index1][k]==4){
                                break;
                            }
                        }
                    }
                    //右上
                    if(i+j<=11){
                        index1=j;
                        for (int k = i-1; k >0 ; k--) {
                            if(Chessboard[k][++index1]==0){
                                sum=sum+1;
                            }else if(Chessboard[k][index1]==1||Chessboard[k][index1]==-1||Chessboard[k][index1]==4){
                                break;
                            }
                        }
                    }else {
                        index1=i;
                        for (int k = j+1; k < 11; k++) {
                            if(Chessboard[--index1][k]==0){
                                sum=sum+1;
                            }else if(Chessboard[index1][k]==1||Chessboard[index1][k]==-1||Chessboard[index1][k]==4){
                                break;
                            }
                        }
                    }
                    //左下
                    if(i+j<=11){
                        index1=i;
                        for (int k = j-1; k > 0; k--) {
                            if(Chessboard[++index1][k]==0){
                                sum=sum+1;
                            }else if(Chessboard[index1][k]==1||Chessboard[index1][k]==-1||Chessboard[index1][k]==4){
                                break;
                            }
                        }
                    }else {
                        index1=j;
                        for (int k = i+1; k < 11; k++) {
                            if(Chessboard[k][--index1]==0){
                                sum=sum+1;
                            }else if(Chessboard[k][index1]==1||Chessboard[k][index1]==-1||Chessboard[k][index1]==4){
                                break;
                            }
                        }
                    }
                    //找出走数最小的棋子
                    if(sum!=0&&sum<flag){
                        flag=sum;
                        beginendchess.setX(i);
                        beginendchess.setY(j);
                    }
                    System.out.println("chess="+i+j+"sum="+sum);
                }
            }
        }
        System.out.println("chess="+beginendchess.getX()+beginendchess.getY()+"flag="+flag);
        System.out.println("index="+index);
        return beginendchess;

    }

    //所有的可行落子位置集合
    public ArrayList endlist(beginendChess beginChess){
        //所有可行落子集合
        ArrayList<beginendChess> endlist = new ArrayList();
        int sum=0;
        int index1;
        int i,j;
        i = beginChess.getX();
        j = beginChess.getY();

        if(Chessboard[i][j]==1){
            //上
            for (int m = i-1; m>0 ; m--) {
                if(Chessboard[m][j]==0){
                    sum=sum+1;
                    beginendChess beginendchess = new beginendChess();
                    beginendchess.setX(m);
                    beginendchess.setY(j);
                    endlist.add(beginendchess);
                }else if(Chessboard[m][j]==1||Chessboard[m][j]==-1||Chessboard[m][j]==4){
                    break;
                }
            }

            //下
            for (int k = i+1; k < 11; k++) {
                if(Chessboard[k][j]==0){
                    sum=sum+1;
                    beginendChess beginendchess = new beginendChess();
                    beginendchess.setX(k);
                    beginendchess.setY(j);
                    endlist.add(beginendchess);
                }else if(Chessboard[k][j]==1||Chessboard[k][j]==-1||Chessboard[k][j]==4){
                    break;
                }
            }

            //左
            for (int k = j-1; k >0; k--) {
                if(Chessboard[i][k]==0){
                    sum=sum+1;
                    beginendChess beginendchess = new beginendChess();
                    beginendchess.setX(i);
                    beginendchess.setY(k);
                    endlist.add(beginendchess);
                }else if(Chessboard[i][k]==1||Chessboard[i][k]==-1||Chessboard[i][k]==4){
                    break;
                }
            }
            //右
            for (int k = j+1; k < 11; k++) {
                if(Chessboard[i][k]==0){
                    sum=sum+1;
                    beginendChess beginendchess = new beginendChess();
                    beginendchess.setX(i);
                    beginendchess.setY(k);
                    endlist.add(beginendchess);
                }else if(Chessboard[i][k]==1||Chessboard[i][k]==-1||Chessboard[i][k]==4){
                    break;
                }
            }

            //左上
            if(i>=j){
                index1=i;
                for (int k = j-1; k > 0; k--) {
                    if(Chessboard[--index1][k]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(index1);
                        beginendchess.setY(k);
                        endlist.add(beginendchess);
                    }else if(Chessboard[index1][k]==1||Chessboard[index1][k]==-1||Chessboard[index1][k]==4){
                        break;
                    }
                }
            }else{
                index1=j;
                for (int k = i-1; k > 0; k--) {
                    if(Chessboard[k][--index1]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(k);
                        beginendchess.setY(index1);
                        endlist.add(beginendchess);
                    }else if(Chessboard[k][index1]==1||Chessboard[k][index1]==-1||Chessboard[k][index1]==4){
                        break;
                    }
                }
            }
            //右下
            if(i>=j){
                index1=j;
                for (int k = i+1; k < 11; k++) {
                    if(Chessboard[k][++index1]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(k);
                        beginendchess.setY(index1);
                        endlist.add(beginendchess);
                    }else if(Chessboard[k][index1]==1||Chessboard[k][index1]==-1||Chessboard[k][index1]==4){
                        break;
                    }
                }
            }else {
                index1=i;
                for (int k = j+1; k < 11; k++) {
                    if(Chessboard[++index1][k]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(index1);
                        beginendchess.setY(k);
                        endlist.add(beginendchess);
                    }else if(Chessboard[index1][k]==1||Chessboard[index1][k]==-1||Chessboard[index1][k]==4){
                        break;
                    }
                }
            }
            //右上
            if(i+j<=11){
                index1=j;
                for (int k = i-1; k >0 ; k--) {
                    if(Chessboard[k][++index1]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(k);
                        beginendchess.setY(index1);
                        endlist.add(beginendchess);
                    }else if(Chessboard[k][index1]==1||Chessboard[k][index1]==-1||Chessboard[k][index1]==4){
                        break;
                    }
                }
            }else {
                index1=i;
                for (int k = j+1; k < 11; k++) {
                    if(Chessboard[--index1][k]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(index1);
                        beginendchess.setY(k);
                        endlist.add(beginendchess);
                    }else if(Chessboard[index1][k]==1||Chessboard[index1][k]==-1||Chessboard[index1][k]==4){
                        break;
                    }
                }
            }

            //左下
            if(i+j<=11){
                index1=i;
                for (int k = j-1; k > 0; k--) {
                    if(Chessboard[++index1][k]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(index1);
                        beginendchess.setY(k);
                        endlist.add(beginendchess);
                    }else if(Chessboard[index1][k]==1||Chessboard[index1][k]==-1||Chessboard[index1][k]==4){
                        break;
                    }
                }
            }else {
                index1=j;
                for (int k = i+1; k < 11; k++) {
                    if(Chessboard[k][--index1]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(k);
                        beginendchess.setY(index1);
                        endlist.add(beginendchess);
                    }else if(Chessboard[k][index1]==1||Chessboard[k][index1]==-1||Chessboard[k][index1]==4){
                        break;
                    }
                }
            }
        }

        return endlist;
    }

    //所有的对方可行落子位置集合
    public ArrayList peoplelist(beginendChess beginChess){
        //所有可行落子集合
        ArrayList<beginendChess> endlist = new ArrayList();
        int sum=0;
        int index1;
        int i,j;
        i = beginChess.getX();
        j = beginChess.getY();

        if(Chessboard[i][j]==-1){
            //上
            for (int m = i-1; m>0 ; m--) {
                if(Chessboard[m][j]==0){
                    sum=sum+1;
                    beginendChess beginendchess = new beginendChess();
                    beginendchess.setX(m);
                    beginendchess.setY(j);
                    endlist.add(beginendchess);
                }else if(Chessboard[m][j]==1||Chessboard[m][j]==-1||Chessboard[m][j]==4){
                    break;
                }
            }

            //下
            for (int k = i+1; k < 11; k++) {
                if(Chessboard[k][j]==0){
                    sum=sum+1;
                    beginendChess beginendchess = new beginendChess();
                    beginendchess.setX(k);
                    beginendchess.setY(j);
                    endlist.add(beginendchess);
                }else if(Chessboard[k][j]==1||Chessboard[k][j]==-1||Chessboard[k][j]==4){
                    break;
                }
            }

            //左
            for (int k = j-1; k >0; k--) {
                if(Chessboard[i][k]==0){
                    sum=sum+1;
                    beginendChess beginendchess = new beginendChess();
                    beginendchess.setX(i);
                    beginendchess.setY(k);
                    endlist.add(beginendchess);
                }else if(Chessboard[i][k]==1||Chessboard[i][k]==-1||Chessboard[i][k]==4){
                    break;
                }
            }
            //右
            for (int k = j+1; k < 11; k++) {
                if(Chessboard[i][k]==0){
                    sum=sum+1;
                    beginendChess beginendchess = new beginendChess();
                    beginendchess.setX(i);
                    beginendchess.setY(k);
                    endlist.add(beginendchess);
                }else if(Chessboard[i][k]==1||Chessboard[i][k]==-1||Chessboard[i][k]==4){
                    break;
                }
            }

            //左上
            if(i>=j){
                index1=i;
                for (int k = j-1; k > 0; k--) {
                    if(Chessboard[--index1][k]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(index1);
                        beginendchess.setY(k);
                        endlist.add(beginendchess);
                    }else if(Chessboard[index1][k]==1||Chessboard[index1][k]==-1||Chessboard[index1][k]==4){
                        break;
                    }
                }
            }else{
                index1=j;
                for (int k = i-1; k > 0; k--) {
                    if(Chessboard[k][--index1]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(k);
                        beginendchess.setY(index1);
                        endlist.add(beginendchess);
                    }else if(Chessboard[k][index1]==1||Chessboard[k][index1]==-1||Chessboard[k][index1]==4){
                        break;
                    }
                }
            }
            //右下
            if(i>=j){
                index1=j;
                for (int k = i+1; k < 11; k++) {
                    if(Chessboard[k][++index1]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(k);
                        beginendchess.setY(index1);
                        endlist.add(beginendchess);
                    }else if(Chessboard[k][index1]==1||Chessboard[k][index1]==-1||Chessboard[k][index1]==4){
                        break;
                    }
                }
            }else {
                index1=i;
                for (int k = j+1; k < 11; k++) {
                    if(Chessboard[++index1][k]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(index1);
                        beginendchess.setY(k);
                        endlist.add(beginendchess);
                    }else if(Chessboard[index1][k]==1||Chessboard[index1][k]==-1||Chessboard[index1][k]==4){
                        break;
                    }
                }
            }
            //右上
            if(i+j<=11){
                index1=j;
                for (int k = i-1; k >0 ; k--) {
                    if(Chessboard[k][++index1]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(k);
                        beginendchess.setY(index1);
                        endlist.add(beginendchess);
                    }else if(Chessboard[k][index1]==1||Chessboard[k][index1]==-1||Chessboard[k][index1]==4){
                        break;
                    }
                }
            }else {
                index1=i;
                for (int k = j+1; k < 11; k++) {
                    if(Chessboard[--index1][k]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(index1);
                        beginendchess.setY(k);
                        endlist.add(beginendchess);
                    }else if(Chessboard[index1][k]==1||Chessboard[index1][k]==-1||Chessboard[index1][k]==4){
                        break;
                    }
                }
            }
            //左下
            if(i+j<=11){
                index1=i;
                for (int k = j-1; k > 0; k--) {
                    if(Chessboard[++index1][k]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(index1);
                        beginendchess.setY(k);
                        endlist.add(beginendchess);
                    }else if(Chessboard[index1][k]==1||Chessboard[index1][k]==-1||Chessboard[index1][k]==4){
                        break;
                    }
                }
            }else {
                index1=j;
                for (int k = i+1; k < 11; k++) {
                    if(Chessboard[k][--index1]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(k);
                        beginendchess.setY(index1);
                        endlist.add(beginendchess);
                    }else if(Chessboard[k][index1]==1||Chessboard[k][index1]==-1||Chessboard[k][index1]==4){
                        break;
                    }
                }
            }
        }

        return endlist;
    }

    //当前棋子可行步数
    public int currentSum(beginendChess current,int[][] chessB){
        //所有可行落子集合
        ArrayList<beginendChess> endlist = new ArrayList();
        int sum=0;
        int index1;
        int i,j;
        i = current.getX();
        j = current.getY();

        if(chessB[i][j]==1){
            //上
            for (int m = i-1; m>0 ; m--) {
                if(chessB[m][j]==0){
                    sum=sum+1;
                    beginendChess beginendchess = new beginendChess();
                    beginendchess.setX(m);
                    beginendchess.setY(j);
                    endlist.add(beginendchess);
                }else if(chessB[m][j]==1||chessB[m][j]==-1||chessB[m][j]==4){
                    break;
                }
            }

            //下
            for (int k = i+1; k < 11; k++) {
                if(chessB[k][j]==0){
                    sum=sum+1;
                    beginendChess beginendchess = new beginendChess();
                    beginendchess.setX(k);
                    beginendchess.setY(j);
                    endlist.add(beginendchess);
                }else if(chessB[k][j]==1||chessB[k][j]==-1||chessB[k][j]==4){
                    break;
                }
            }

            //左
            for (int k = j-1; k >0; k--) {
                if(chessB[i][k]==0){
                    sum=sum+1;
                    beginendChess beginendchess = new beginendChess();
                    beginendchess.setX(i);
                    beginendchess.setY(k);
                    endlist.add(beginendchess);
                }else if(chessB[i][k]==1||chessB[i][k]==-1||chessB[i][k]==4){
                    break;
                }
            }
            //右
            for (int k = j+1; k < 11; k++) {
                if(chessB[i][k]==0){
                    sum=sum+1;
                    beginendChess beginendchess = new beginendChess();
                    beginendchess.setX(i);
                    beginendchess.setY(k);
                    endlist.add(beginendchess);
                }else if(chessB[i][k]==1||chessB[i][k]==-1||chessB[i][k]==4){
                    break;
                }
            }

            //左上
            if(i>=j){
                index1=i;
                for (int k = j-1; k > 0; k--) {
                    if(chessB[--index1][k]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(index1);
                        beginendchess.setY(k);
                        endlist.add(beginendchess);
                    }else if(chessB[index1][k]==1||chessB[index1][k]==-1||chessB[index1][k]==4){
                        break;
                    }
                }
            }else{
                index1=j;
                for (int k = i-1; k > 0; k--) {
                    if(chessB[k][--index1]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(k);
                        beginendchess.setY(index1);
                        endlist.add(beginendchess);
                    }else if(chessB[k][index1]==1||chessB[k][index1]==-1||chessB[k][index1]==4){
                        break;
                    }
                }
            }
            //右下
            if(i>=j){
                index1=j;
                for (int k = i+1; k < 11; k++) {
                    if(chessB[k][++index1]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(k);
                        beginendchess.setY(index1);
                        endlist.add(beginendchess);
                    }else if(chessB[k][index1]==1||chessB[k][index1]==-1||chessB[k][index1]==4){
                        break;
                    }
                }
            }else {
                index1=i;
                for (int k = j+1; k < 11; k++) {
                    if(chessB[++index1][k]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(index1);
                        beginendchess.setY(k);
                        endlist.add(beginendchess);
                    }else if(chessB[index1][k]==1||chessB[index1][k]==-1||chessB[index1][k]==4){
                        break;
                    }
                }
            }
            //右上
            if(i+j<=11){
                index1=j;
                for (int k = i-1; k >0 ; k--) {
                    if(chessB[k][++index1]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(k);
                        beginendchess.setY(index1);
                        endlist.add(beginendchess);
                    }else if(chessB[k][index1]==1||chessB[k][index1]==-1||chessB[k][index1]==4){
                        break;
                    }
                }
            }else {
                index1=i;
                for (int k = j+1; k < 11; k++) {
                    if(chessB[--index1][k]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(index1);
                        beginendchess.setY(k);
                        endlist.add(beginendchess);
                    }else if(chessB[index1][k]==1||chessB[index1][k]==-1||chessB[index1][k]==4){
                        break;
                    }
                }
            }
            //左下
            if(i+j<=11){
                index1=i;
                for (int k = j-1; k > 0; k--) {
                    if(chessB[++index1][k]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(index1);
                        beginendchess.setY(k);
                        endlist.add(beginendchess);
                    }else if(chessB[index1][k]==1||chessB[index1][k]==-1||chessB[index1][k]==4){
                        break;
                    }
                }
            }else {
                index1=j;
                for (int k = i+1; k < 11; k++) {
                    if(chessB[k][--index1]==0){
                        sum=sum+1;
                        beginendChess beginendchess = new beginendChess();
                        beginendchess.setX(k);
                        beginendchess.setY(index1);
                        endlist.add(beginendchess);
                    }else if(chessB[k][index1]==1||chessB[k][index1]==-1||chessB[k][index1]==4){
                        break;
                    }
                }
            }
        }

        return sum;
    }

    //落子
    public beginendChess eChess(beginendChess beginChess){

        ArrayList<beginendChess> list = new ArrayList<beginendChess>();
        beginendChess end = new beginendChess();
        int[][] chessb = new int[12][12];
        int sum = 0,sum1 = 0;
        //所有可落子集合
        list = endlist(beginChess);

        //第一次,模拟当前落子时局面的好坏情况
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 12; j++) {
                chessb[i][j] = Chessboard[i][j];
            }
        }

        chessb[beginChess.getX()][beginChess.getY()] = 0;
        end = list.get(0);
        chessb[end.getX()][end.getY()] = 1;
        sum = currentSum(end,chessb);

        //第二次往后，模拟当前落子时局面的好坏情况，找出当前最有利位置
        for (int i = 1; i < list.size(); i++) {
            for (int j = 0; j < 12; j++) {
                for (int k = 0; k < 12; k++) {
                    chessb[j][k] = Chessboard[j][k];
                }
            }
            chessb[beginChess.getX()][beginChess.getY()] = 0;
            chessb[list.get(i).getX()][list.get(i).getY()] = 1;

            sum1 = currentSum(list.get(i),chessb);
            if(sum<sum1){
                end = list.get(i);
                sum = sum1;
            }
        }
        return end;
    }

    //障碍落子位置
    public beginendChess barrier(beginendChess endChess){
            ArrayList<beginendChess> endlist = new ArrayList<beginendChess>();
            //两个标记数组
            Map<beginendChess, ArrayList<beginendChess>> map=new HashMap<>();
            Map<beginendChess,Integer> map1=new HashMap<>();

            int [][] chess = new int[12][12];
            //棋盘
            for (int i = 0; i < 12; i++) {
                for (int j = 0; j < 12; j++) {
                    chess[i][j] = Chessboard[i][j];
                }
            }

            //可放障碍位
            endlist = endlist(endChess);
        System.out.println("end="+endChess.getX()+"="+endChess.getY()+endlist.size());

        for (int i = 1; i <11 ; i++) {
            for (int j = 1; j < 11; j++) {
                if(chess[i][j] == -1){
                    int sum=0;
                    beginendChess other = new beginendChess();
                    other.setX(i);
                    other.setY(j);
                    map.put(other,new ArrayList<beginendChess>());

                    if(chess[i][j+1]==0){
                        for (int k = 0; k < endlist.size(); k++) {
                            if(endlist.get(k).getX()==i&&endlist.get(k).getY()==j+1){
                                beginendChess barr = new beginendChess();
                                barr.setX(i);
                                barr.setY(j+1);
                                map.get(other).add(barr);
                            }
                        }
                    }else if(chess[i][j+1]==4){
                        beginendChess barr = new beginendChess();
                        barr.setX(i);
                        barr.setY(j+1);
                        sum=sum+1;
                    }

                    if(chess[i][j-1]==0){
                        for (int k = 0; k < endlist.size(); k++) {
                            if(endlist.get(k).getX()==i&&endlist.get(k).getY()==j-1){
                                beginendChess barr = new beginendChess();
                                barr.setX(i);
                                barr.setY(j-1);
                                map.get(other).add(barr);
                            }
                        }
                    }else if(chess[i][j-1]==4){
                        beginendChess barr = new beginendChess();
                        barr.setX(i);
                        barr.setY(j-1);
                        sum=sum+1;
                    }

                    if(chess[i+1][j]==0){
                        for (int k = 0; k < endlist.size(); k++) {
                            if(endlist.get(k).getX()==i+1&&endlist.get(k).getY()==j){
                                beginendChess barr = new beginendChess();
                                barr.setX(i+1);
                                barr.setY(j);
                                map.get(other).add(barr);
                            }
                        }
                    }else if(chess[i+1][j]==4){
                        beginendChess barr = new beginendChess();
                        barr.setX(i+1);
                        barr.setY(j);
                        sum=sum+1;
                    }

                    if(chess[i-1][j]==0){
                        for (int k = 0; k < endlist.size(); k++) {
                            if(endlist.get(k).getX()==i-1&&endlist.get(k).getY()==j){
                                beginendChess barr = new beginendChess();
                                barr.setX(i-1);
                                barr.setY(j);
                                map.get(other).add(barr);
                            }
                        }
                    }else if(chess[i-1][j]==4){
                        beginendChess barr = new beginendChess();
                        barr.setX(i-1);
                        barr.setY(j);
                        sum=sum+1;
                    }

                    if(chess[i+1][j+1]==0){
                        for (int k = 0; k < endlist.size(); k++) {
                            if(endlist.get(k).getX()==i+1&&endlist.get(k).getY()==j+1){
                                beginendChess barr = new beginendChess();
                                barr.setX(i+1);
                                barr.setY(j+1);
                                map.get(other).add(barr);
                            }
                        }
                    }else if(chess[i+1][j+1]==4){
                        beginendChess barr = new beginendChess();
                        barr.setX(i+1);
                        barr.setY(j+1);
                        sum=sum+1;
                    }


                    if(chess[i-1][j-1]==0){
                        for (int k = 0; k < endlist.size(); k++) {
                            if(endlist.get(k).getX()==i-1&&endlist.get(k).getY()==j-1){
                                beginendChess barr = new beginendChess();
                                barr.setX(i-1);
                                barr.setY(j-1);
                                map.get(other).add(barr);
                            }
                        }
                    }else if(chess[i-1][j-1]==4){
                        beginendChess barr = new beginendChess();
                        barr.setX(i-1);
                        barr.setY(j-1);
                        sum=sum+1;
                    }

                    if(chess[i+1][j-1]==0){
                        for (int k = 0; k < endlist.size(); k++) {
                            if(endlist.get(k).getX()==i+1&&endlist.get(k).getY()==j-1){
                                beginendChess barr = new beginendChess();
                                barr.setX(i+1);
                                barr.setY(j-1);
                                map.get(other).add(barr);
                            }
                        }
                    }else if(chess[i+1][j-1]==4){
                        beginendChess barr = new beginendChess();
                        barr.setX(i+1);
                        barr.setY(j-1);
                        sum=sum+1;
                    }

                    if(chess[i-1][j+1]==0){
                        for (int k = 0; k < endlist.size(); k++) {
                            if(endlist.get(k).getX()==i-1&&endlist.get(k).getY()==j+1){
                                beginendChess barr = new beginendChess();
                                barr.setX(i-1);
                                barr.setY(j+1);
                                map.get(other).add(barr);
                            }
                        }
                    }else if(chess[i-1][j+1]==4){
                        beginendChess barr = new beginendChess();
                        barr.setX(i-1);
                        barr.setY(j+1);
                        sum=sum+1;
                    }
                    map1.put(other,sum);
                }
            }
        }

        Iterator<Map.Entry<beginendChess, ArrayList<beginendChess>>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<beginendChess, ArrayList<beginendChess>> entry = iterator.next();


//            System.out.println("xy:"+entry.getKey().getX()+entry.getKey().getY());
//            System.out.println("大小："+entry.getValue().size());

            if(entry.getValue().size()==0){
                map1.remove(entry.getKey());
            }
        }

        if(map1.size()!=0){
            int tag=0;
            //最终的落子
            beginendChess barra = new beginendChess();
            beginendChess barra1 = new beginendChess();
            Iterator<Map.Entry<beginendChess, Integer>> iterator1 = map1.entrySet().iterator();

            while (iterator1.hasNext()) {
                Map.Entry<beginendChess, Integer> entry = iterator1.next();
                if(entry.getValue()>tag){
                    tag = entry.getValue();
                    barra = entry.getKey();
                    System.out.println("tag="+tag);
                    System.out.println("x--y="+barra.getX()+barra.getY());

                }
                if(tag==0){
                    tag = entry.getValue();
                    barra = entry.getKey();
                }
                System.out.println("xy="+entry.getKey().getX()+entry.getKey().getY());
                System.out.println("大小="+entry.getValue());
                System.out.println();

            }

            System.out.println("zuihou="+barra.getX()+barra.getY());
            barra1.setX(map.get(barra).get(0).getX());
            barra1.setY(map.get(barra).get(0).getY());

            System.out.println("最终落子："+barra1.getX()+barra1.getY());
            return barra1;
        }else {
            beginendChess zhangai = new beginendChess();
            int three=0;
            for (int i = 1; i < 11; i++) {
                for (int j = 1; j < 11; j++) {
                    //敌方棋子
                    if(chess[i][j]==-1){
                        ArrayList<beginendChess> array  = new ArrayList<>();
                        beginendChess barr = new beginendChess();
                        barr.setX(i);
                        barr.setY(j);
                        //所有对方棋子可行位置
                        array = peoplelist(barr);
                        for (int k = 0; k < array.size(); k++) {
                            for (int l = 0; l < endlist.size(); l++) {
                                if(endlist.get(l).getX()==array.get(k).getX()&&endlist.get(l).getY()==array.get(k).getY()){
                                    three=three+1;
                                    zhangai = endlist.get(l);
                                    System.out.println("障碍："+zhangai.getX()+zhangai.getY());
                                }
                            }
                        }
                    }
                }
            }
                if(three==0){
                        zhangai=endlist.get(endlist.size()-1);
                }
            return zhangai;
        }

    }
}


